


package jnpf.property.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.exception.DataException;
import jnpf.permission.entity.UserEntity;
import jnpf.property.entity.ZcglPropertyRepairEntity;
import jnpf.property.entity.ZcglPropertyRepairItemEntity;
import jnpf.property.model.zcglpropertyrepair.*;
import jnpf.property.service.ZcglPropertyRepairItemService;
import jnpf.property.service.ZcglPropertyRepairService;
import jnpf.util.GeneraterSwapUtil;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;


/**
 * ZcglPropertyRepair
 *
 * @版本： V3.2.0
 * @版权： 智果云科技（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2024-11-14
 */
@Slf4j
@RestController
@Api(tags = "ZcglPropertyRepair", value = "example")
@RequestMapping("/api/property/repair")
public class ZcglPropertyRepairController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private ZcglPropertyRepairService zcglPropertyRepairService;

    @Autowired
    private ZcglPropertyRepairItemService zcglPropertyRepairItemService;


    /**
     * 列表
     *
     * @param zcglPropertyRepairPagination
     * @return
     */
    @ApiOperation("获取列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZcglPropertyRepairPagination zcglPropertyRepairPagination) throws IOException {
        List<ZcglPropertyRepairEntity> list = zcglPropertyRepairService.getList(zcglPropertyRepairPagination);
        //处理id字段转名称，若无需转或者为空可删除

        List<ZcglPropertyRepairListVO> listVO = JsonUtil.getJsonToList(list, ZcglPropertyRepairListVO.class);
        for (ZcglPropertyRepairListVO zcglPropertyRepairVO : listVO) {
            zcglPropertyRepairVO.setMaintenanceUser_name(zcglPropertyRepairVO.getMaintenanceUser());

            zcglPropertyRepairVO.setStartTime_name(zcglPropertyRepairVO.getStartTime());

            zcglPropertyRepairVO.setEndTime_name(zcglPropertyRepairVO.getEndTime());

            zcglPropertyRepairVO.setHandlingContent_name(zcglPropertyRepairVO.getHandlingContent());

            zcglPropertyRepairVO.setHandlingFile_name(generaterSwapUtil.getFileNameInJson(zcglPropertyRepairVO.getHandlingFile()));

            //子表数据转换
            List<ZcglPropertyRepairItemEntity> zcglPropertyRepairItemList = zcglPropertyRepairService.getZcglPropertyRepairItemList(zcglPropertyRepairVO.getId(), zcglPropertyRepairPagination);
            List<ZcglPropertyRepairItemModel> zcglPropertyRepairItemModelList = JsonUtil.getJsonToList(zcglPropertyRepairItemList, ZcglPropertyRepairItemModel.class);
            for (ZcglPropertyRepairItemModel zcglPropertyRepairItemModel : zcglPropertyRepairItemModelList) {
            }
            zcglPropertyRepairVO.setZcglPropertyRepairItemList(zcglPropertyRepairItemModelList);
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zcglPropertyRepairPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);

    }

    /**
     * 创建
     *
     * @param zcglPropertyRepairForm
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid ZcglPropertyRepairForm zcglPropertyRepairForm) throws DataException {
        boolean b = zcglPropertyRepairService.checkForm(zcglPropertyRepairForm, 0);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        String mainId = RandomUtil.uuId();

        UserInfo userInfo = userProvider.get();
        UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        ZcglPropertyRepairEntity entity = JsonUtil.getJsonToBean(zcglPropertyRepairForm, ZcglPropertyRepairEntity.class);
        entity.setId(mainId);
        entity.setStatus(1);
        zcglPropertyRepairService.save(entity);
        if (zcglPropertyRepairForm.getZcglpropertyrepairitemList() != null) {
            List<ZcglPropertyRepairItemEntity> ZcglPropertyRepairItemList = JsonUtil.getJsonToList(zcglPropertyRepairForm.getZcglpropertyrepairitemList(), ZcglPropertyRepairItemEntity.class);
            for (ZcglPropertyRepairItemEntity entitys : ZcglPropertyRepairItemList) {
                entitys.setId(RandomUtil.uuId());
                entitys.setParentid(entity.getId());

                zcglPropertyRepairItemService.save(entitys);
            }
        }


        return ActionResult.success("创建成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<ZcglPropertyRepairInfoVO> info(@PathVariable("id") String id) {
        ZcglPropertyRepairEntity entity = zcglPropertyRepairService.getInfo(id);
        ZcglPropertyRepairInfoVO vo = JsonUtil.getJsonToBean(entity, ZcglPropertyRepairInfoVO.class);

        //子表
        List<ZcglPropertyRepairItemEntity> ZcglPropertyRepairItemList = zcglPropertyRepairService.getZcglPropertyRepairItemList(id);
        for (ZcglPropertyRepairItemEntity zcglpropertyrepairitemEntity : ZcglPropertyRepairItemList) {
        }
        vo.setZcglPropertyRepairItemList(JsonUtil.getJsonToList(ZcglPropertyRepairItemList, ZcglPropertyRepairItemModel.class));
        //副表
        return ActionResult.success(vo);
    }

    /**
     * 表单信息(详情页)
     *
     * @param id
     * @return
     */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<ZcglPropertyRepairInfoVO> detailInfo(@PathVariable("id") String id) {
        ZcglPropertyRepairEntity entity = zcglPropertyRepairService.getInfo(id);
        System.out.println(entity+"1111");
        ZcglPropertyRepairInfoVO vo = JsonUtil.getJsonToBean(entity, ZcglPropertyRepairInfoVO.class);

        //子表数据转换
        List<ZcglPropertyRepairItemEntity> zcglPropertyRepairItemList = zcglPropertyRepairService.getZcglPropertyRepairItemList(id);
        List<ZcglPropertyRepairItemModel> zcglPropertyRepairItemModelList = JsonUtil.getJsonToList(zcglPropertyRepairItemList, ZcglPropertyRepairItemModel.class);
        for (ZcglPropertyRepairItemModel zcglPropertyRepairItemModel : zcglPropertyRepairItemModelList) {
        }
        vo.setZcglPropertyRepairItemList(zcglPropertyRepairItemModelList);
        //附表数据转换
        //添加到详情表单对象中
        vo.setMaintenanceUser(vo.getMaintenanceUser());
        vo.setStartTime(vo.getStartTime());
        vo.setEndTime(vo.getEndTime());
        vo.setHandlingContent(vo.getHandlingContent());
        vo.setHandlingFile(generaterSwapUtil.getFileNameInJson(vo.getHandlingFile()));
        return ActionResult.success(vo);
    }

    /**
     * 不带子表更新：作废和挂起
     *
     * @param id
     * @param zcglPropertyRepairForm
     * @return
     */
    @PutMapping("/updateOne/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult updateOne(@PathVariable("id") String id, @RequestBody @Valid ZcglPropertyRepairForm zcglPropertyRepairForm) throws DataException {
        boolean b = zcglPropertyRepairService.checkForm(zcglPropertyRepairForm, 1);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        UserInfo userInfo = userProvider.get();
        ZcglPropertyRepairEntity entity = zcglPropertyRepairService.getInfo(id);
        if (entity != null) {
            ZcglPropertyRepairEntity subentity = JsonUtil.getJsonToBean(zcglPropertyRepairForm, ZcglPropertyRepairEntity.class);
            boolean b1 = zcglPropertyRepairService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }

            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

    /**
     * 更新
     *
     * @param id
     * @param zcglPropertyRepairForm
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZcglPropertyRepairForm zcglPropertyRepairForm) throws DataException {
        boolean b = zcglPropertyRepairService.checkForm(zcglPropertyRepairForm, 1);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        UserInfo userInfo = userProvider.get();
        ZcglPropertyRepairEntity entity = zcglPropertyRepairService.getInfo(id);
        if (entity != null) {
            ZcglPropertyRepairEntity subentity = JsonUtil.getJsonToBean(zcglPropertyRepairForm, ZcglPropertyRepairEntity.class);
            boolean b1 = zcglPropertyRepairService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }

            QueryWrapper<ZcglPropertyRepairItemEntity> ZcglPropertyRepairItemqueryWrapper = new QueryWrapper<>();
            ZcglPropertyRepairItemqueryWrapper.lambda().eq(ZcglPropertyRepairItemEntity::getParentid, entity.getId());
            zcglPropertyRepairItemService.remove(ZcglPropertyRepairItemqueryWrapper);
            if (zcglPropertyRepairForm.getZcglpropertyrepairitemList() != null) {
                List<ZcglPropertyRepairItemEntity> ZcglPropertyRepairItemList = JsonUtil.getJsonToList(zcglPropertyRepairForm.getZcglpropertyrepairitemList(), ZcglPropertyRepairItemEntity.class);
                for (ZcglPropertyRepairItemEntity entitys : ZcglPropertyRepairItemList) {


                    entitys.setId(RandomUtil.uuId());
                    entitys.setParentid(entity.getId());
                    zcglPropertyRepairItemService.save(entitys);
                }
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZcglPropertyRepairEntity entity = zcglPropertyRepairService.getInfo(id);
        if (entity != null) {
            zcglPropertyRepairService.delete(entity);
            QueryWrapper<ZcglPropertyRepairItemEntity> queryWrapperZcglPropertyRepairItem = new QueryWrapper<>();
            queryWrapperZcglPropertyRepairItem.lambda().eq(ZcglPropertyRepairItemEntity::getParentid, entity.getId());

            zcglPropertyRepairItemService.remove(queryWrapperZcglPropertyRepairItem);

        }
        return ActionResult.success("删除成功");
    }


}
